package org.micro.frame.oss;

import com.qiniu.common.Zone;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * The Elasticsearch Auto Configuration
 *
 * @author lry
 * @since 1.0.0
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(QiNiuOSSProperties.class)
public class QiNiuOSSAutoConfiguration {

    private final QiNiuOSSProperties properties;

    public QiNiuOSSAutoConfiguration(QiNiuOSSProperties properties) {
        this.properties = properties;
    }

    @Bean
    public Auth auth() {
        return Auth.create(properties.getAccessKey(), properties.getSecretKey());
    }

    @Bean
    @ConditionalOnMissingBean
    public UploadManager uploadManager() throws Exception {
        QiNiuOSSProperties.Configuration config = properties.getConfiguration();
        com.qiniu.storage.Configuration configuration = new com.qiniu.storage.Configuration();
        if (config.getZone() == null || config.getZone().length() == 0) {
            configuration.zone = Zone.zone0();
        } else {
            configuration.zone = (Zone) Zone.class.getMethod(config.getZone()).invoke(null);
        }
        if (config.getUseHttpsDomains() != null) {
            configuration.useHttpsDomains = config.getUseHttpsDomains();
        }
        if (config.getPutThreshold() != null) {
            configuration.putThreshold = config.getPutThreshold();
        }
        if (config.getConnectTimeout() != null) {
            configuration.connectTimeout = config.getConnectTimeout();
        }
        if (config.getWriteTimeout() != null) {
            configuration.writeTimeout = config.getWriteTimeout();
        }
        if (config.getReadTimeout() != null) {
            configuration.readTimeout = config.getReadTimeout();
        }
        if (config.getDispatcherMaxRequests() != null) {
            configuration.dispatcherMaxRequests = config.getDispatcherMaxRequests();
        }
        if (config.getDispatcherMaxRequestsPerHost() != null) {
            configuration.dispatcherMaxRequestsPerHost = config.getDispatcherMaxRequestsPerHost();
        }
        if (config.getConnectionPoolMaxIdleCount() != null) {
            configuration.connectionPoolMaxIdleCount = config.getConnectionPoolMaxIdleCount();
        }
        if (config.getConnectionPoolMaxIdleMinutes() != null) {
            configuration.connectionPoolMaxIdleMinutes = config.getConnectionPoolMaxIdleMinutes();
        }
        if (config.getRetryMax() != null) {
            configuration.retryMax = config.getRetryMax();
        }
        if (config.getUseDnsHostFirst() != null) {
            configuration.useDnsHostFirst = config.getUseDnsHostFirst();
        }

        return new UploadManager(configuration);
    }

}
